// pages/thumbsUp/index.js
const app = getApp();
import {
	goBack,
	PageJump,
	dateFormat
} from '../../Utils/Utils'

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		CustomBar: app.globalData.CustomBar,
		StatusBar: app.globalData.StatusBar, // 状态栏高度
		windowHeight: app.globalData.windowHeight, // 屏幕整体高度
		scrollHeight: 0, // scroll高度
		limit: 5,
		newsList: [],
		BombBoxShow: false, // 加载状态是否显示
		BombBoxText: "努力加载中", // 加载状态文字
		BombBox2Show: false, // 提示状态是否显示
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let that = this
		let CustomBar = that.data.CustomBar
		let windowHeight = that.data.windowHeight
		let temp = windowHeight - CustomBar
		that.setData({
			scrollHeight: temp
		})
		that.getPraiseInfo()
		// 将用户未读的 标为已读
		that.isreadToRead()
	},
	isreadToRead(){
		// 获取本人未读的点赞信息
		wx.cloud.callFunction({
			name: "operation",
			data:{
				function: "getPraiseNoReadNum",
				isread: false
			}
		}).then(res=>{
			if(res.result.list.length > 0){
				for (const list of res.result.list) {
					wx.cloud.callFunction({
						name: "operation",
						data:{
							function: "setIsread",
							collection: "praise",
							_id: list._id
						}
					}).then(res=>{
					}).catch(err=>{
						console.log(err)
					})
				}
			}
		}).catch(err=>{
			console.log(err)
		})
	},

	/**
	 * 获取点赞信息
	 */
	getPraiseInfo() {
		let that = this;
		that.openBombBox("加载中...");
		let skip = that.data.newsList.length
		let limit = that.data.limit
		if (skip % limit != 0) {
			that.closeBombBox()
			that.optionBombBox2Show("没有了^_$_^")
			return false
		}
		wx.cloud.callFunction({
			name: "operation",
			data: {
				function: "getPraiseInfoByOpenid",
				skip: 0,
				limit: 10
			}
		}).then(res => {
			let praiseList = res.result.list
			let newList = []
			for (const praise of praiseList) {
				let user = praise.user[0]
				let acticle = praise.acticle[0]
				newList.push({
					id: praise._id,
					name: user.nickName,
					time: dateFormat("YYYY-mm-dd HH:MM", new Date(praise.timestamp)),
					img: user.avatarUrl,
					info: {
						essayid: acticle._id,
						type: acticle.type,
						text: acticle.text
					}
				})
			}
			that.setData({
				newsList: that.data.newsList.concat(newList)
			})
			this.closeBombBox()
		}).catch(err => {
			console.log(err)
		})
	},


	optionBombBox2Show: function (param) {
		let that = this
		let temp = that.data.BombBox2Show
		let temp2 = that.data.BombBoxShow
		if (temp || temp2) {
			return
		}
		that.setData({
			BombBox2Show: true,
			BombBox2text: param
		})
		setTimeout(() => {
			that.setData({
				BombBox2Show: false,
			})
		}, 3000)
	},

	openBombBox: function (param) {
		let that = this
		that.setData({
			BombBoxShow: true,
			BombBoxText: param
		})
	},

	closeBombBox: function () {
		let that = this
		that.setData({
			BombBoxShow: false,
		})
	},

	goBack,

	goUserDetail: function (e) { // 跳转评论者详情
		let {
			id
		} = e.currentTarget.dataset
		PageJump('userDetail', 'id', id)
	},

	goCommentDetail: function (e) { // 跳转被评论文章 ( 传递文章ID )
		let {
			type,
			essayid
		} = e.currentTarget.dataset
		if (type == 1) {
			PageJump('detailExpress', '_id', essayid)
		} else if (type == 2) {
			PageJump('detailBeUnusedToSell', '_id', essayid)
		} else if (type == 3) {
			PageJump('detailLoseAndReceive', '_id', essayid)
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})